package io.aicn.lowcode.controller;

import io.aicn.lowcode.entity.Position;
import io.aicn.lowcode.service.IPositionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Tag(name = "职位管理", description = "职位信息管理API")
@RestController
@RequestMapping("/api/positions")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @Operation(summary = "获取所有职位", description = "获取系统中所有职位信息")
    @GetMapping
    public List<Position> getAllPositions() {
        return positionService.list();
    }

    @Operation(summary = "根据ID获取职位", description = "根据职位ID获取职位详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Position> getPositionById(@PathVariable Long id) {
        Position position = positionService.getById(id);
        if (position != null) {
            return ResponseEntity.ok(position);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "搜索职位", description = "根据条件搜索职位")
    @GetMapping("/search")
    public List<Position> searchPositions(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String code,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) BigDecimal minSalary,
            @RequestParam(required = false) BigDecimal maxSalary,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) String status) {
        return positionService.searchPositions(title, code, category, minSalary, maxSalary, level, status);
    }

    @Operation(summary = "创建职位", description = "创建新的职位")
    @PostMapping
    public ResponseEntity<Position> createPosition(@RequestBody Position position) {
        Position createdPosition = positionService.createPosition(position);
        return ResponseEntity.ok(createdPosition);
    }

    @Operation(summary = "更新职位", description = "更新现有职位信息")
    @PutMapping("/{id}")
    public ResponseEntity<Position> updatePosition(@PathVariable Long id, @RequestBody Position position) {
        Position updatedPosition = positionService.updatePosition(id, position);
        if (updatedPosition != null) {
            return ResponseEntity.ok(updatedPosition);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Operation(summary = "删除职位", description = "根据ID删除职位")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePosition(@PathVariable Long id) {
        boolean deleted = positionService.deletePosition(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}